import axios from "axios";

export const useWorkType = (type) => !type ? '' : type === 'in' ? '入库' : '出库';

export const useHeaderCellStyle = () => {
  return {
    fontSize: `${12}px`,
    fontWeight: 'bold',
    color: '#8c8c90',
    backgroundColor: 'rgb(239, 243, 247)'
  }
}

const netHttp = 'http://125.42.155.175:8000';
const headers = {
  'Content-Type': 'application/json'
}

export function useHttp(url, data, http = netHttp) {
  return axios.post(`${http}${url}`, data, {
    headers
  });
}

export function useHttpGet(url, data, http = netHttp) {
  return axios.get(`${http}${url}`, data, {
    headers
  });
}

export function exportExcelHttp(url, query, http) {
  return axios({
    url: `${http}${url}`,
    method: 'post',
    data: query,
    parameter: {
      type: 'WMS'
    },
    responseType: 'blob'
  });
}

export function useCustomer(_this) {
  const {customerInfoList} = _this.$utils.getUserInfo();
  return customerInfoList[0].customerPartyCode;
}

export function useRealName(_this) {
  return _this.$utils.getUserInfo().realName;
}

export function useCustomerName(_this) {
  const {customerInfoList} = _this.$utils.getUserInfo();
  return customerInfoList[0].customerPartyName;
}

export const useDate = (date, fmt = 'yyyy-MM-dd') => {
  if (!date) {
    console.error('不是一个日期')
    return '/';
  }
  date = new Date(date);
  const o = {
    "M+": date.getMonth() + 1,
    "d+": date.getDate(),
    "h+": date.getHours(),
    "m+": date.getMinutes(),
    "s+": date.getSeconds(),
    "q+": Math.floor((date.getMonth() + 3) / 3),
    "S": date.getMilliseconds()
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (const k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
};

export function useDict(value, list) {
  const item = list.find(item => item.dictValue == value);
  return item && item.dictName;
}

export function useRandom(n, m) {
  return Math.floor(Math.random() * (m + 1 - n) + n);
}

export function thousandthPercentile(data, decimal = 3) {
  if (!data) {
    return 0;
  }
  data = typeof data === 'string' ? +data : data;
  data.toFixed(decimal);
  data = data.toString();
  const list = data.split('.');
  const repReg = /(?=(\B)(\d{3})+$)/g;
  list[0] = list[0].replace(repReg, ',');
  return list.join('.');
}
